home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 29 < prev    next >
Text File  |  1996-08-06  |  2KB  |  63 lines

  1. Newsgroups: comp.std.c
  2. Path: howland.reston.ans.net!torn!sq!msb
  3. From: msb@sq.com (Mark Brader)
  4. Subject: Re: sizeof(1L) in preprocesor;How to tell sizeof(double)>sizeof(long) ?
  5. Message-ID: <1996Jan5.094122.16151@sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <sc3f9vb6gk.fsf@lns101.lns.cornell.edu>
  8. Date: Fri, 5 Jan 1996 09:41:22 GMT
  9.  
  10. Nobuhiko Katayama (nk@lns598.lns.cornell.edu) writes:
  11. > Dear experts,
  12. >     I understand that sizeof(double) cannot be used with #if as
  13. > double is a type name.
  14.  
  15. This is not the reason.  The reason is that the standard requires a
  16. C program to be processed in specific sequence of 8 logical phases
  17. of translation.  The #if directive is evaluated during translation
  18. phase 4, but the identifiers "sizeof" and "double" are not understood
  19. to be keywords until translation phase 7.
  20.  
  21. > Could you tell me where in the standard it says
  22. > that I cannot use sizeof(1.0) or sizeof(1L)... ?
  23.  
  24. The same reason applies.
  25.  
  26. Section 5.1.1.2 of the standard (2.1.1.2 in the original ANSI version)
  27. defines the translation phases, and section 6.8.1 (3.8.1) describes
  28. the #if directive.  In particular, note the footnote where it points
  29. out that in the expression inside a #if, there are only identifiers
  30. and not keywords.
  31.  
  32. > is there any portable way to tell whether
  33. > long is longer(shorter) than double in preprocessor stage ?
  34.  
  35. I can't think of any way.
  36.  
  37. You *can* use the various characteristics defined in <limits.h> and
  38. <float.h>, as explained in section 5.2.4.2 (2.2.4.2), to find out
  39. various information about the types.  In particular, if what you
  40. really want is to choose whichever of the two types can hold the
  41. larger exact integer value, then in practice you need only do:
  42.  
  43.     #include <limits.h>
  44.     #include <float.h>
  45.     #if LONG_MAX * DBL_EPSILON > 1
  46.         typedef long mytype;
  47.         #define mytype_format "%ld" /* for printf */
  48.     #else
  49.         typedef double mytype;
  50.         #define mytype_format "%f"
  51.     #endif
  52.  
  53. This may not work if the number of bits in a long is close to the
  54. number of bits in the significand of a double -- I don't want to
  55. think about the exact criteria -- but in practice that won't happen
  56. on any likely machine.
  57. -- 
  58. Mark Brader             | "As penance, I suppose I should read the standard
  59. msb@sq.com              |  again, but I've already lost as much hair as
  60. SoftQuad Inc., Toronto  |  I can afford."             -- Tom Kelly
  61.  
  62. My text in this article is in the public domain.
  63.